using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BikyBall.Graphic;

namespace BikyBall.Graphic.Constructor
{
    static class CubeConstructor
    {
        public static VertexPositionNormalTexture[] _vertices = null;
        public static VertexBuffer _vertexBuffer = null;
        public static Texture2D _texture = null;
        public static GraphicProperty Construct()
        {
            GraphicProperty graphicProperty = new GraphicProperty();
            const int NUM_TRIANGLES = 12;
            const int NUM_VERTICES = 36;
            if (_vertices == null)
            {
                _vertices = new VertexPositionNormalTexture[NUM_VERTICES];

                // Calculate the position of the vertices on the top face.
                Vector3 topLeftFront = new Vector3(-0.5f, 0.5f, -0.5f);
                Vector3 topLeftBack = new Vector3(-0.5f, 0.5f, 0.5f);
                Vector3 topRightFront = new Vector3(0.5f, 0.5f, -0.5f);
                Vector3 topRightBack = new Vector3(0.5f, 0.5f, 0.5f);

                // Calculate the position of the vertices on the bottom face.
                Vector3 btmLeftFront = new Vector3(-0.5f, -0.5f, -0.5f);
                Vector3 btmLeftBack = new Vector3(-0.5f, -0.5f, 0.5f);
                Vector3 btmRightFront = new Vector3(0.5f, -0.5f, -0.5f);
                Vector3 btmRightBack = new Vector3(0.5f, -0.5f, 0.5f);

                // Normal vectors for each face (needed for lighting / display)
                Vector3 normalFront = new Vector3(0.0f, 0.0f, 1.0f);
                Vector3 normalBack = new Vector3(0.0f, 0.0f, -1.0f);
                Vector3 normalTop = new Vector3(0.0f, 1.0f, 0.0f);
                Vector3 normalBottom = new Vector3(0.0f, -1.0f, 0.0f);
                Vector3 normalLeft = new Vector3(-1.0f, 0.0f, 0.0f);
                Vector3 normalRight = new Vector3(1.0f, 0.0f, 0.0f);

                // UV texture coordinates
                Vector2 textureTopLeft = new Vector2(0.0f, 0.0f);
                Vector2 textureTopRight = new Vector2(1.0f, 0.0f);
                Vector2 textureBottomLeft = new Vector2(0.0f, 1.0f);
                Vector2 textureBottomRight = new Vector2(1.0f, 1.0f);
                Vector2 shift = new Vector2(1.0f/4.0f, 0.0f);

                // Add the _vertices for the FRONT face.
                _vertices[0] = new VertexPositionNormalTexture(topLeftFront, normalFront, textureTopLeft);
                _vertices[1] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
                _vertices[2] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);
                _vertices[3] = new VertexPositionNormalTexture(btmLeftFront, normalFront, textureBottomLeft);
                _vertices[4] = new VertexPositionNormalTexture(btmRightFront, normalFront, textureBottomRight);
                _vertices[5] = new VertexPositionNormalTexture(topRightFront, normalFront, textureTopRight);

                // Add the _vertices for the BACK face.
                _vertices[6] = new VertexPositionNormalTexture(topLeftBack, normalBack, textureTopRight + shift);
                _vertices[7] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft + shift);
                _vertices[8] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight + shift);
                _vertices[9] = new VertexPositionNormalTexture(btmLeftBack, normalBack, textureBottomRight + shift);
                _vertices[10] = new VertexPositionNormalTexture(topRightBack, normalBack, textureTopLeft + shift);
                _vertices[11] = new VertexPositionNormalTexture(btmRightBack, normalBack, textureBottomLeft + shift);

                // Add the _vertices for the TOP face.
                _vertices[12] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft + shift);
                _vertices[13] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight + shift);
                _vertices[14] = new VertexPositionNormalTexture(topLeftBack, normalTop, textureTopLeft + shift);
                _vertices[15] = new VertexPositionNormalTexture(topLeftFront, normalTop, textureBottomLeft + shift);
                _vertices[16] = new VertexPositionNormalTexture(topRightFront, normalTop, textureBottomRight + shift);
                _vertices[17] = new VertexPositionNormalTexture(topRightBack, normalTop, textureTopRight + shift);

                // Add the _vertices for the BOTTOM face. 
                _vertices[18] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
                _vertices[19] = new VertexPositionNormalTexture(btmLeftBack, normalBottom, textureBottomLeft);
                _vertices[20] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
                _vertices[21] = new VertexPositionNormalTexture(btmLeftFront, normalBottom, textureTopLeft);
                _vertices[22] = new VertexPositionNormalTexture(btmRightBack, normalBottom, textureBottomRight);
                _vertices[23] = new VertexPositionNormalTexture(btmRightFront, normalBottom, textureTopRight);

                // Add the _vertices for the LEFT face.
                _vertices[24] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);
                _vertices[25] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
                _vertices[26] = new VertexPositionNormalTexture(btmLeftFront, normalLeft, textureBottomRight);
                _vertices[27] = new VertexPositionNormalTexture(topLeftBack, normalLeft, textureTopLeft);
                _vertices[28] = new VertexPositionNormalTexture(btmLeftBack, normalLeft, textureBottomLeft);
                _vertices[29] = new VertexPositionNormalTexture(topLeftFront, normalLeft, textureTopRight);

                // Add the _vertices for the RIGHT face. 
                _vertices[30] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft + shift);
                _vertices[31] = new VertexPositionNormalTexture(btmRightFront, normalRight, textureBottomLeft + shift);
                _vertices[32] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight + shift);
                _vertices[33] = new VertexPositionNormalTexture(topRightBack, normalRight, textureTopRight + shift);
                _vertices[34] = new VertexPositionNormalTexture(topRightFront, normalRight, textureTopLeft + shift);
                _vertices[35] = new VertexPositionNormalTexture(btmRightBack, normalRight, textureBottomRight + shift);
                _vertexBuffer = new VertexBuffer(GraphicEngine.Device, VertexPositionNormalTexture.VertexDeclaration, NUM_VERTICES, BufferUsage.WriteOnly);
                _vertexBuffer.SetData(_vertices);
                _texture = GraphicEngine.Content.Load<Texture2D>("ombre");
            }
            graphicProperty.Vertices = _vertices;
            graphicProperty.NumVertices = NUM_VERTICES;
            graphicProperty.NumTriangles = NUM_TRIANGLES;
            graphicProperty.PrimitiveType = PrimitiveType.TriangleList;
            graphicProperty.VertexBuffer = _vertexBuffer;
            graphicProperty.Texture = _texture;
            graphicProperty.Effect = new BasicEffect(GraphicEngine.Device);
            graphicProperty.Effect.TextureEnabled = true;
            graphicProperty.Effect.Texture = _texture;
            graphicProperty.Effect.EnableDefaultLighting();
            graphicProperty.IndexBuffer = null;
            return graphicProperty;
        }
    }
}
